package com.rambo.baselib.user;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.rambo.baselib.ResourceTable;
import com.rambo.baselib.constant.ParamConstant;
import com.rambo.baselib.fraction.BaseFraction;
import com.rambo.baselib.msgbus.EventBus;
import com.rambo.baselib.mvp.BasePresenter;
import com.rambo.baselib.util.PrefUtil;
import com.rambo.baselib.util.ToastUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.app.Context;

/***********************************************************************
 * <p>@description:用户的信息
 * <p>@author: rambo
 * <p>@version: 1.0.0
 * <p>@created on: 2022/1/19 15:45
 **********************************************************************/
public class UserFraction extends BaseFraction {
    Image ivIcon;
    Text tvUserName, tvLogin, tvRegister;

    public UserFraction(Context context) {
        super(context);
    }

    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_ability_fraction_user;
    }

    @Override
    protected void initView(Component component) {
        super.initView(component);
        ivIcon = (Image) component.findComponentById(ResourceTable.Id_iv_icon);
        tvUserName = (Text) component.findComponentById(ResourceTable.Id_tv_user_name);
        tvLogin = (Text) component.findComponentById(ResourceTable.Id_tv_login);
        tvRegister = (Text) component.findComponentById(ResourceTable.Id_tv_register);

        Glide.with(context)
                .asDrawable()
                .load(ResourceTable.Media_user_default)
                .circleCrop()
                .into(ivIcon);
        verifyLoginState();
    }
    private void verifyLoginState(){
        //已登陆
        if (PrefUtil.getInstance().getBoolean(ParamConstant.PREF_KEY.USER_IS_LOGIN)) {
            tvLogin.setVisibility(Component.HIDE);
            tvRegister.setVisibility(Component.HIDE);
            tvUserName.setText(PrefUtil.getInstance().getString(ParamConstant.PREF_KEY.USER_NAME));

            Glide.with(context)
                    .asDrawable()
                    .error(ResourceTable.Media_user_default)
                    .load(PrefUtil.getInstance().getString(ParamConstant.PREF_KEY.USER_ICON))
                    .circleCrop()
                    .into(ivIcon);
        }
    }
    @Override
    protected void registerClick() {
        super.registerClick();
        ivIcon.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (!PrefUtil.getInstance().getBoolean(ParamConstant.PREF_KEY.USER_IS_LOGIN)) {
                    ToastUtil.toast("未登陆");
                    return;
                }
                //TODO 选一张图片或者拍一张图片上传
                ToastUtil.toast("选一张图片或者拍一张图片上传");
            }
        });
        tvRegister.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent.OperationBuilder operationBuilder=new Intent.OperationBuilder()
                        .withBundleName(getBundleName())
                        .withAbilityName(RegisterAbility.class.getName())
                        .withDeviceId("");
                Intent intent=new Intent();
                intent.setOperation(operationBuilder.build());
                startAbility(intent,0);
            }
        });
        tvLogin.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent.OperationBuilder operationBuilder=new Intent.OperationBuilder()
                        .withBundleName(getBundleName())
                        .withAbilityName(LoginAbility.class.getName())
                        .withDeviceId("");
                Intent intent=new Intent();
                intent.setOperation(operationBuilder.build());
                startAbility(intent,0);
            }
        });
    }

    @Override
    protected void onEventReceive(int type, Object eventBean) {
        super.onEventReceive(type, eventBean);
        if(ParamConstant.EVENT_TYPE.LOGIN_SUCCESS==type){
            verifyLoginState();
        }
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
